#-*-coding:utf-8-*-
# 整数反转
# 每次取最后一位数
from day03 import calRunTime
@calRunTime
def reversedNum(num):
	ret = 0
	count = 0
	while(num):
		count+=1
		ret = num%10+ret*10
		if num <0:
			return 0
		num /= 10
	print "count{}".format(count)
	return ret

## 回文数

def isPalindrome(num):
	numStr = str(num)
	ret = True
	numCount = len(numStr)
	for idx in xrange(numCount):
		if numStr[idx] != numStr[numCount-1-idx]:
			ret = False
	return ret

# 冒泡排序
# 两两比较相邻元素，如果前一个比后一个大，则交换
#
@calRunTime
def myBubbleSort(tmpList):
	listCount = len(tmpList)
	for i in xrange(0,listCount):
		for j in xrange(0,listCount-i-1):
			if tmpList[j] > tmpList[j+1]:
				tmpList[j],tmpList[j+1] = tmpList[j+1],tmpList[j]

	return tmpList
# 选择排序
# 在一次遍历中把最小的放在最前面
@calRunTime
def mySelectSort(tmpList):
	listCount = len(tmpList)
	for i in xrange(0,listCount):
		for j in xrange(i,listCount):
			if tmpList[i] > tmpList[j]:
				tmpList[j],tmpList[i] = tmpList[i],tmpList[j]

	return tmpList

# 插入排序
# 在未排序的数据中从后往前，查找到合适的位置然后放进去
@calRunTime
def myInsertSort(tmpList):
	listCount = len(tmpList)
	for i in xrange(0,listCount):
		for j in xrange(i,1,-1):
			if tmpList[j] < tmpList[j-1]:
				tmpList[j],tmpList[j-1] = tmpList[j-1],tmpList[j]
	return tmpList


#  2的次幂
# 2的次幂的整数一定满足一下条件:
# n >0 and n&m-1 == 0
@calRunTime
def isPowerOfTwo(num):
	ret = False
	if num <= 0:
		return ret

	return num&(num-1)==0


if __name__ == "__main__":
	#print(reversedNum(123457809))
	#print(isPalindrome(12321))
	print(myBubbleSort([1,5,8,1,2,6,9,10]))
	print(mySelectSort([1,5,8,1,2,6,9,10]))
	print(myInsertSort([1,5,8,1,2,6,9,10]))
	print isPowerOfTwo(81)
	print bin(5,2)
